<template>
	<view>
		<bar :nav="setNav" />
		<van-popup :show="chooseWeek" @close="onCloseChooseWeek" position="bottom" custom-class="height:50%">
			<van-picker show-toolbar title="请选择现在的周数" :columns="weeks" @cancel="cancelChooseWeek" @confirm="confirmWeek"
				:loading="loadingTable" />
		</van-popup>
		<van-row>
			<van-col span="22" offset="2">
				<van-grid :border="false" :column-num="7" :clickable="true">
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周一
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周二
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周三
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周四
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周五
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周六
						</van-tag>
					</van-grid-item>
					<van-grid-item use-slot custom-class="week">
						<van-tag type="primary" color="#006600" custom-class="weekTag">
							周日
						</van-tag>
					</van-grid-item>
				</van-grid>
			</van-col>
		</van-row>
		<van-row>
			<van-col span="2">
				<van-grid :border="false" :column-num="1" :clickable="true">
					<van-grid-item use-slot v-for="value in 5" :key="value" custom-class="courseNum">
						<div class="course">
							<van-button type="primary" color="#006600" block custom-class="courseNumBtn">
								{{value+1}}
							</van-button>
						</div>
					</van-grid-item>
				</van-grid>
			</van-col>
			<van-radio-group :value="unit" @change="addClass">
				<van-grid :border="false" :column-num="7" :clickable="true">
					<van-grid-item v-for="value in 35" :key="value" use-slot custom-class="editCourse">
						<van-button type="primary" color="#006600" v-show="map[value]!==-1?true:false"
							@click="editCourse(value)"
							custom-style="padding-left:5rpx;line-height:35rpx;padding-right:5rpx;padding-top:5rpx;height:203rpx;margin: 0 auto;width:90rpx;border-radius: 10rpx;font-size:25rpx!important;font-weight:bold">
							<div>{{InfoMap[value].courseName}}</div>
						</van-button>
						<van-radio use-icon-slot :value="unit" :name="value" v-show="map[value]!==-1?false:true">
							<image style="margin-top:10rpx;height:190rpx;width:90rpx;" slot="icon"
								:src="unit===value?icon.choosed:icon.unChoosed" />
						</van-radio>

					</van-grid-item>
				</van-grid>
			</van-radio-group>
			<van-notify id="van-notify" />
		</van-row>
	</view>
</template>
<script>
	import bar from '../../components/bar.vue';
	import Notify from '../../wxcomponents/vant/dist/notify/notify';
	export default {
		components: {
			bar
		},
		data() {
			return {
				loadingTable: false,
				courseMap: [],
				map: [],
				InfoMap: [],
				weeks: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
				unit: null,
				showBtn: false,
				chooseWeek: false,
				week: 1,
				icon: {
					unChoosed: "/static/Btn/addBtn.png",
					choosed: "/static/Btn/addBtn_HL.png"
				},
				setNav: {
					'bg': '#006600',
					'color': "#fff",
					'isdisPlayNavTitle': true,
					'navTitle': '课表'
				},
				windowHeight: null,
				weekHeight: null,
				courseHeight: null,
				btnHeight: null
			}
		},
		mounted() {
			let token = ""
			token = wx.getStorageSync('token')
			if (token !== "") {
				this.loadDefaultData()
			} else {
				this.login()
			}
		},
		onLoad() {
			uni.$on('choose', (chooseWeek) => {
				this.chooseWeek = chooseWeek
			})
		},
		onShow() {
			this.loadDefaultData()
		},
		onUnload() {
			uni.$off('choose')
		},
		methods: {
			login() {
				let self = this
				wx.login({
					success(res) {
						wx.request({
							url: getApp().globalData.baseURL + "/login",
							data: {
								"code": res.code
							},
							success(res) {
								if (res.data.code === 1) {
									wx.setStorageSync('openid', res.data.data.openid)
									wx.setStorageSync('token', res.data.data.token)
									self.loadDefaultData()
								} else {
									Notify({
										type: 'danger',
										message: '获取用户信息失败,请重试'
									});
								}

							},
							fail(err) {
								Notify({
									type: 'danger',
									message: '连接服务器失败'
								});
							}
						})
					},
					fail(err) {
						Notify({
							type: 'danger',
							message: '获取用户登录态失败'
						});
					}
				})
			},

			editCourse(value) {

				var Info = this.InfoMap[value]
				uni.$on('Send', () => {
					uni.$emit('courseInfo', Info)
				})
				uni.navigateTo({
					url: "../courseInfo/courseInfo"
				})
			},
			// 关闭选择周数弹出层
			onCloseChooseWeek() {
				this.chooseWeek = false
			},
			cancelChooseWeek(e) {
				this.chooseWeek = false
			},
			// 计算并得到一个包含课程信息的数组
			getMap(data) {
				var map = new Array()
				for (let i = 0; i < data.length; i++) {
					map[i] = {
						unit: (data[i].jieshu - 1) * 7 + data[i].day - 1,
						courseName: data[i].courseName,
						teacher: data[i].teacher,
						classroom: data[i].classroom,
						weeks: data[i].weeks
					}
				}
				this.courseMap = map
			},
			resetMap(courseMap) {
				// 拿到一个排序的数组
				var map = new Array()
				for (let i = 0; i < courseMap.length; i++) {
					map[i] = courseMap[i].unit
				}
				var compare = function(x, y) {
					if (x < y) {
						return -1;
					} else if (x > y) {
						return 1;
					} else {
						return 0;
					}
				}
				map.sort(compare)
				var Map = new Array(35).fill(-1)
				for (let i = 0; i < map.length; i++) {
					Map[map[i]] = map[i]
				}
				this.map = Map
			},
			getInfoMap(courseMap) {
				var map = new Array()

				function compare(prop) {
					return function(x, y) {
						var v1 = x[prop];
						var v2 = y[prop];
						return v1 - v2
					}
				}
				map = courseMap.sort(compare('unit'))
				// console.log(map)
				var Map = new Array(35).fill({})
				for (let i = 0; i < map.length; i++) {
					Map[map[i].unit] = {
						unit: map[i].unit,
						teacher: map[i].teacher,
						classroom: map[i].classroom,
						courseName: map[i].courseName,
						weeks: map[i].weeks
					}
				}
				this.InfoMap = Map

			},
			loadDefaultData() {
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/course/selectByOpenidAndWeeks",
					data: {
						"openid": wx.getStorageSync('openid'),
						"week": `${this.week}`
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						self.loadingTable = false
						if (res.data.code == 1) {
							self.getMap(res.data.data)
							self.resetMap(self.courseMap)
							self.getInfoMap(self.courseMap)
						} else if (res.data.code == 0) {
							Notify({
								type: 'warning',
								message: '您尚未登录,无法获取课表哦',
								top: 64
							});
						} else {
							Notify({
								type: 'danger',
								message: '获取课表失败',
								top: 64
							});
						}
						this.chooseWeek = false
					},
					fail: (err) => {
						self.loadingTable = false
						Notify({
							type: 'danger',
							message: '获取课表失败,请检查您的网络连接',
							top: 64
						});
						this.chooseWeek = false
					},

				})
			},
			confirmWeek(e) {
				this.week = e.detail.value
				this.loadingTable = true
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/course/selectByOpenidAndWeeks",
					data: {
						"openid": wx.getStorageSync('openid'),
						"week": `${this.week}`
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						self.loadingTable = false
						if (res.data.code == 1) {
							self.getMap(res.data.data)
							self.resetMap(self.courseMap)
							self.getInfoMap(self.courseMap)
							Notify({
								type: 'success',
								message: '获取课表成功!',
								top: 64
							});
						} else if (res.data.code == 0) {
							Notify({
								type: 'warning',
								message: '您尚未登录,无法获取课表哦',
								top: 64
							});
						} else {
							Notify({
								type: 'danger',
								message: '获取课表失败',
								top: 64
							});
						}
						this.chooseWeek = false
						console.log(res.data)
					},
					fail: (err) => {
						self.loadingTable = false
						Notify({
							type: 'danger',
							message: '获取课表失败,请检查您的网络连接',
							top: 64
						});
						this.chooseWeek = false
					},

				})
			},
			// 添加课程
			addClass(e) {
				this.unit = e.detail
				uni.navigateTo({
					url: "../addCourse/addCourse"
				})
				uni.$on('send', () => {
					uni.$emit('update', {
						msg: this.unit
					})
				})

			}
		}
	}
</script>
<style>
	.month {
		/* height: 60rpx; */
		font-size: 32rpx;
	}

	.week {
		height: 60rpx;
		text-align: center;
	}

	.courseNum {
		height: 210rpx;

		text-align: center;
	}

	.courseNumBtn {
		height: 203rpx !important;
		width: 20rpx !important;
		border-radius: 10rpx !important;
	}

	.editCourse {
		height: 210rpx;
	}

	.weekTag {
		width: 60rpx;
		height: 50rpx;
		text-align: center;
		font-weight: bold;
	}

	.course {
		width: 100rpx;
		margin-right: 20rpx;
	}
</style>
